<html>
<head><title>Calculator</title></head>
<body>

<h1>Calculator</h1>
(Ver by Min Sung Kim)<br />
Type an expression in the following box (e.g., 10.5+20*3/25).
<p>
<form action="./calculator.php" method="GET">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>
</p>

<?php
  error_reporting(0);
  $expr = $_GET["expr"]; 
  if($expr) {
    preg_match("#([-]*\d+(\.\d+)*)+([-+*/][-]*\d+(\.\d+)*)*#", $expr, $matches);
    echo "<h2>Result</h2>";
    if ($matches[0] == $expr) {
      eval("\$ans = $matches[0];");
      echo $matches[0]." = ".$ans."\n";
    } else echo "Invalid Input Expression!"."\n";
  }
?>
</body>
</html>

